﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ViCode_LeVi.Data;

namespace ViCode_LeVi.Controls
{
    /// <summary>
    /// Interaction logic for VMProjectInfo.xaml
    /// </summary>
    public partial class VMProjectInfo : UserControl
    {
        public VMProjectInfo()
        {
            InitializeComponent();
            SetupEvent();
            SetupDataContext(ProjectData.Intance);
        }
        ProjectData _ProjectData_Org;
        public void SetupDataContext(ProjectData data)
        {
            //if (data == null)
            //{
            //    MessageBox.Show("data == null. Xin thông báo tới chúng tôi để sửa lỗi");
            //    return;
            //}
            _ProjectData_Org = data;
            ProjectData newData = data == null ? new ProjectData() : CopyProperties(data);
            this.DataContext = newData;
        }
        private ProjectData CopyProperties(ProjectData d1)
        {
            ProjectData d2 = new ProjectData();
            CopyProperties(d1, d2);
            return d2;
        }
        private void CopyProperties(ProjectData d1, ProjectData d2)
        {
            d2.Author = d1.Author;
            d2.Comment = d1.Comment;
            d2.CompanyName = d1.CompanyName;
            d2.Name = d1.Name;
            d2.StartedDate_Date = d1.StartedDate_Date;
            d2.SendProperty_ProjectInfo();
        }
        private void SetupEvent()
        {
            Button_OK.Click += new RoutedEventHandler(Button_OK_Click);
            Button_Cancel.Click += new RoutedEventHandler(Button_Cancel_Click);
        }
        public Action<ProjectData> Action_AddNew;
        void Button_OK_Click(object sender, RoutedEventArgs e)
        {
            ProjectData current = this.DataContext as ProjectData;
            if (current == null)
            {
                MessageBox.Show("current == null. Xin thông báo tới chúng tôi để sửa lỗi");
            }
            if (_ProjectData_Org != null)
            {
                if (_ProjectData_Org == null)
                {
                    MessageBox.Show("_ProjectData_Org == null. Xin thông báo tới chúng tôi để sửa lỗi");
                }
                CopyProperties(current, _ProjectData_Org);                
            }
            else
            {
                if (Action_AddNew != null)
                {
                    Action_AddNew(current);
                }
            }
            Close();
        }

        void Button_Cancel_Click(object sender, RoutedEventArgs e)
        {

            Close();
        }

        private void Close()
        {
            var w = DependencyHelper.GetAncestorByTypeOrBaseType(this, typeof(Window)) as Window;
            if (w != null)
                w.Close();
        }
    }
}
